<?php


namespace app\controllers\h5\uni;

use app\controllers\h5\base\H5Controller;
use app\models\QueueRepeat;
use app\models\QueueScanPay;
use app\models\QueueScanPayPool;
use app\models\ShopImage;
use app\models\user\User;

class MoneyController extends H5Controller
{
    // 获取店家资料
    public function actionGetMain()
    {
        $model = User::find()
            ->where(['id' => $this->user->id])
            ->select(['id', 'mobile', 'is_merchant', 'merchant_name', 'merchant_avatar', 'merchant_address'])
            ->asArray()
            ->one();

        return $this->successEncode($model);
    }

    // 保存
    public function actionSaveMain()
    {
        $data = $this->post();

        $model = User::findOne(['id' => $this->user->id]);

        $model->merchant_name = $data['merchant_name'];
        $model->merchant_address = $data['merchant_address'];
        $model->save();

        return $this->success();
    }

    // 删除图片
    public function actionRemoveMain()
    {
        $model = User::findOne(['id' => $this->user->id]);
        $model->merchant_avatar = '';
        $model->save();

        return $this->success();
    }

    /* ------------------------ */

    public function actionProductList()
    {
        $list = ShopImage::find()
            ->where(['user_id' => $this->user->id])
            ->andWhere(['status' => 1])
            ->with(['image'])
            ->orderBy(['id' => SORT_ASC])
            ->asArray()
            ->all();

        return $this->successEncode(['list' => $list]);
    }

    public function actionRemoveProduct()
    {
        $image_id = $this->post('image_id');

        $model = ShopImage::findOne(['image_id' => $image_id, 'user_id' => $this->user->id]);
        if ($model) {
            $model->status = 0;
            $model->save();
        }

        return $this->success();
    }

    // 首页商家
    public function actionHomeShop()
    {
        $list = User::find()
            ->where(['is_merchant' => 1])
            ->andWhere(['not', ['merchant_avatar' => ['', null]]])
            ->select(['id', 'merchant_avatar', 'merchant_address', 'mobile', 'merchant_name'])
            ->orderBy(['update_time' => SORT_DESC])
            ->limit(40)
            ->asArray()
            ->all();

        return $this->successEncode(['list' => $list]);
    }

    public function actionShopView()
    {
        $id = $this->post('id');

        $model = User::find()
            ->where(['is_merchant' => 1])
            ->andWhere(['id' => $id])
            ->select(['id', 'merchant_avatar', 'merchant_address', 'mobile', 'merchant_name'])
            ->asArray()
            ->one();
        if ($model) {
            $model['product'] = ShopImage::find()
                ->where(['user_id' => $id])
                ->andWhere(['status' => 1])
                ->with(['image'])
                ->orderBy(['id' => SORT_ASC])
                ->asArray()
                ->all();

            return $this->successEncode($model);
        }

        return $this->fail();
    }

    public function actionShopAll()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = User::find()
            ->where(['is_merchant' => 1])
            ->andWhere(['not', ['merchant_avatar' => ['', null]]])
            ->select(['id', 'merchant_avatar', 'merchant_address', 'mobile', 'merchant_name']);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['update_time' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        foreach ($list as &$item) {
            $item['product'] = ShopImage::find()
                ->where(['user_id' => $item['id']])
                ->andWhere(['status' => 1])
                ->with(['image'])
                ->orderBy(['id' => SORT_ASC])
                ->asArray()
                ->all();
        }

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    // 排队中奖总数
    public function actionQueueRepeat()
    {
        $total = (int) QueueRepeat::find()
            ->where(['status' => 2])
            ->count();

        return $this->successEncode(['total' => $total]);
    }

    public function actionQueueRepeatList()
    {
        $list = QueueRepeat::find()
            ->where(['status' => 0])
            ->orderBy(['create_time' => SORT_ASC])
            ->with(['user'])
            ->asArray()
            ->all();

        foreach ($list as &$item) {
            if ($item['user']['nickname']) {
                $item['user']['nickname'] = base64_decode($item['user']['nickname']);
            }
        }

        return $this->successEncode(['list' => $list]);
    }

    public function actionQueueScan()
    {
        $id = $this->post('id');

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        // 奖金池
        $total = QueueScanPayPool::find()
            ->where(['merchant_id' => $id])
            ->select(['amount'])
            ->scalar();
        if (!$total) {
            $total = 0;
        }

        // 排队列表
        $query = QueueScanPay::find()
            ->where(['merchant_id' => $id]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['create_time' => SORT_ASC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->with(['user'])
            ->asArray()
            ->all();

        foreach ($list as &$item) {
            if ($item['user']['nickname']) {
                $item['user']['nickname'] = base64_decode($item['user']['nickname']);
            }
        }

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page,
            'total' => $total,
        ]);
    }
}